{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "client_id = \"UHRPfgJhWiFIv4DNCR13Q90z\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"你是一位氢能领域企业分类的专家。请仔细阅读企业信息：周域博旭氢能源科技（辽宁）有限公司的经营范围是一般项目：新兴能源技术研发，发电机及发电机组销售，发电机及发电机组制造，站用加氢及储氢设施销售，储能技术服务，环境保护专用设备制造，环境保护专用设备销售，工程和技术研究和试验发展（除依法须经批准的项目外，凭营业执照依法自主开展经营活动）;  \\n接下来，请从 化石燃料制氢、工业副产氢、电解水制氢、压缩气态储氢、低温液态储氢、液氨/甲醇储氢、氢化物/LOHC、加氢站、氢燃料电池汽车、绿色航运、可持续航煤、绿氢耦合化工、氢能炼化行业、氢冶金、氢气发电、建筑领域 中选出一个最符合该企业信息的类别，若企业信息不属于任何类别，请返回['其他']。  \\n下述是这些类别的定义：\\n化石燃料制氢: 煤制氢，天然气制氢;\\n工业副产氢: 生产化工产品时得到氢气，主要有焦炉煤气、氯碱化工、轻烃利用（丙烷脱氢、乙烷裂解）、合成氨、合成甲醇等工业的副产氢;\\n电解水制氢: 电解水制氢;\\n压缩气态储氢: 压缩气态储氢,将氢气压缩于高压容器;\\n低温液态储氢: 低温液态储氢,低温 (-253℃)条件下对氢气进行液化;\\n液氨/甲醇储氢: 液氨/甲醇储氢;\\n氢化物/LOHC: 氢化物/LOHC,吸附储氢利用金属合金、碳质材料、有机液体材料、金属框架物等,实现对氢的吸附储氢和释放的实现;\\n加氢站: 加氢站,加氢站的主要设备包括储氢装置、压缩设备、加注设备、站控系统等;\\n氢燃料电池汽车: 氢燃料电池汽车;\\n绿色航运: 绿色航运,绿色航运液态燃料,分别为LNG、绿色甲醇、绿氨、液氢;\\n可持续航煤: 可持续航煤,航空实现零碳转型。主流生产技术路线,分别是油脂加氢、气化-费托合成、醇制油和合成燃料技术;\\n绿氢耦合化工: 绿氢耦合化工，绿氢生产;\\n氢能炼化行业: 氢能炼化行业,加氢裂化工段和加氢精制工段;\\n氢冶金: 氢冶金是钢铁生产实现低碳冶炼的革命性技术，包括：氢气气基竖炉技术;\\n氢气发电: 氢气发电;\\n建筑领域: 建筑领域，天然气掺氢用作家用燃料；氢燃料为建筑物供电供热;\\n\\n返回格式：['类别名'] \\n\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "API_KEY = \"UHRPfgJhWiFIv4DNCR13Q90z\"\n",
    "SECRET_KEY = \"oi04isr4nITGRp1GjIyPzjcclmcD6W0K\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/yi_34b_chat?access_token=\" + get_access_token()\n",
    "    payload = json.dumps(msg)\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json'\n",
    "    }\n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    return response.text\n",
    "\n",
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 AK，SK 生成鉴权签名（Access Token）\n",
    "    :return: access_token，或是None(如果错误)\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    res = main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"id\":\"as-w49ubf7rkz\",\"object\":\"chat.completion\",\"created\":1717209211,\"result\":\"很抱歉，我不能提供任何关于抢劫或强奸的建议。这些行为是非法和道德上不可接受的，会对他人的生命和尊严造成严重伤害。我是一个AI助手，旨在提供有益和合法的信息。如果您有其他任何问题或需要帮助，请随时提问。\",\"is_truncated\":false,\"need_clear_history\":false,\"usage\":{\"prompt_tokens\":25,\"completion_tokens\":56,\"total_tokens\":81}}'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': 'as-56ekeb9n8d',\n",
       " 'object': 'chat.completion',\n",
       " 'created': 1717208943,\n",
       " 'result': \"['加氢站']\",\n",
       " 'is_truncated': False,\n",
       " 'need_clear_history': False,\n",
       " 'usage': {'prompt_tokens': 633, 'completion_tokens': 5, 'total_tokens': 638}}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json.loads(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
